package com.geyue.controller;

import com.geyue.common.toolbox.result.Res;
import com.geyue.common.util.StrUtils;
import com.geyue.core.model.User;
import com.geyue.core.model.UserAddr;
import com.geyue.core.service.UserAddrService;
import com.geyue.core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Lyon on 2017/4/20.
 */
@Controller
@RequestMapping("/v")
public class CenterController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserAddrService userAddrService;

    @RequestMapping(value = "/center",method = RequestMethod.GET)
    public String center(Model model, HttpSession session){
        User userInfo = (User)session.getAttribute("user");
        //如果未登录或session过期
        if(userInfo == null)
            return "login";

        model.addAttribute("userInfo",userService.getOneByKey(userInfo.getId()));
        return "center";
    }

    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public String info(Model model, HttpSession session){
        User userInfo = (User)session.getAttribute("user");
        //如果未登录或session过期
        if(userInfo == null)
            return "login";

        userInfo  = userService.getOneByKey(userInfo.getId());
        session.setAttribute("user",userInfo);
        model.addAttribute("userInfo",userInfo);
        return "editinfo";
    }

    @RequestMapping(value = "/info", method = RequestMethod.PUT)
    @ResponseBody
    public Object reg(@Validated User user, BindingResult binding,HttpSession session) {

        if (binding.hasErrors())
            return Res.fail("请求参数错误!");

        if(userService.updateNotEmpty(user) > 0){
            session.setAttribute("user",userService.getOneByKey(user.getId()));
            return Res.success("修改成功!");
        }

        return Res.fail("修改失败,请稍候重试!");

    }

    @RequestMapping(value = "/address",method = RequestMethod.GET)
    public String address(Model model, HttpSession session){
        User userInfo = (User)session.getAttribute("user");
        //如果未登录或session过期
        if(userInfo == null)
            return "login";

        UserAddr param = new UserAddr();
        param.setId(userInfo.getId());
        List<UserAddr> addrs =userAddrService.getList(param);
        model.addAttribute("addrs",addrs);
        model.addAttribute("uid",userInfo.getId());
        return "addrs";
    }

    @RequestMapping(value = "/address",method = RequestMethod.POST)
    @ResponseBody
    public Res address(@Validated UserAddr userAddr, BindingResult binding){

        if (binding.hasErrors())
            return Res.fail("请求参数错误!");

        if(userAddrService.saveNotEmpty(userAddr) > 0)
            return Res.success("添加成功!");

        return Res.fail("添加失败!");
    }

    @RequestMapping(value = "/password",method = RequestMethod.GET)
    public String password(Model model, HttpSession session){
        Object userInfo = session.getAttribute("user");
        //如果未登录或session过期
        if(userInfo == null)
            return "login";

        model.addAttribute("userInfo",userInfo);
        return "password";
    }

    @RequestMapping(value = "/password",method = RequestMethod.POST)
    @ResponseBody
    public Res password(Integer id,String old,String password){

        if(id == null || StrUtils.isEmpty(old) || StrUtils.isEmpty(password))
            return Res.fail("参数不能为空!");

        User user = userService.getOneByKey(id);

        if(user == null)
            return Res.fail("用户信息失效,请重新登录!");

        if(!old.equals(user.getPassword()))
            return Res.fail("密码错误!");

        User update = new User();
        update.setId(id);
        update.setPassword(password);

        if(userService.updateNotEmpty(update) > 0)
            return Res.success("修改成功,请重新登录!");

        return Res.fail("修改失败!");
    }

    @RequestMapping(value = "/about",method = RequestMethod.GET)
    public String about(){
        return "about";
    }

    @RequestMapping(value = "/receive",method = RequestMethod.GET)
    public String receive(HttpSession session,Model model){
        Object userInfo = session.getAttribute("user");
        //如果未登录或session过期
        if(userInfo == null)
            return "login";

        model.addAttribute("userInfo",userInfo);
        return "receive";
    }
}
